<template>
  <div>
    <v-back></v-back>
    <div class="detail">
      <img :src="detalist.img" alt="" />
      <div class="title">
        <!-- 价格 -->
        <span class="price">￥{{ detalist.price | toPrice }}</span
        ><s>￥{{ detalist.market_price | toPrice}}</s>
        <!-- 标题 -->
        <h2>{{ detalist.goodsname }}</h2>
        <p>一些商品的详细介绍</p>
      </div>
      <div v-html="detalist.description"></div>
    </div>

    <!-- 最下面的进入购物车 -->
    <div class="botbox">
      <van-goods-action>
        <van-goods-action-icon icon="chat-o" text="客服" color="#ccc" />
        <van-goods-action-icon icon="cart-o" text="购物车"  @click="gocart()" />
        <van-goods-action-icon icon="star" text="收藏" color="#ccc" />
        <van-goods-action-button
          type="warning"
          text="加入购物车"
          @click="showPopup(detalist.id)"
        />
        <van-goods-action-button type="danger" text="立即购买" />
      </van-goods-action>
    </div>
  </div>
</template>


<script>
import { getgoodsinfo, cartadd } from "../request/app";
export default {
  data() {
    return {
      show: false,
      detalist: [],
    };
  },
  methods: {
    // 加入购物车
    showPopup(id) {
      if(!localStorage.getItem('info')){
        alert('请先登录');
        this.$router.push('/login');
        return;
      };
      this.show = true;

      //这是添加购物车的接口
      cartadd({
        uid: JSON.parse(localStorage.getItem("info")).uid,
        goodsid: id,
        num: 1,
      }).then((val) => {
        if (val.code != 200) {
          this.$toast(val.msg);
          return;
        }
        this.$toast(val.msg);
      });
    },

    // 跳转到购物车
    gocart(){
      this.$router.push('/cart');
    }
  },
  mounted() {
    //获取商品详情的数据
    getgoodsinfo({
      id: this.$route.query.id,
    }).then((val) => {
      this.detalist = val.list[0];
    });
  },
};
</script>


<style scoped>
@import "../assets/css/detail.css";
</style>